<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>vue响应式的原理</title>
</head>
<body>
  <script>
    function cb() {
      console.log('更新视图啦...')
    }
    function defineReactive(obj, key, value) {
      Object.defineProperty(obj,key, {
        enumerable: true,
        configurable: true,
        get: function reactiveGetter() {
          return value
        },
        set: function reactiveSetter(nVal) {
          if(nVal===value) return
          cb()
        }
      })
    }
    function observer(value) {
      if(!value || (typeof value !='object')) {
        return
      }
      Object.keys(value).forEach(key=> {
        defineReactive(value,key,value[key])
      })
    }

    class Vue {
      constructor(options) {
        this._data = options.data
        observer(this._data)
      }
    }

    let vm = new Vue({ data: {age:10}})
    vm._data.age = 20
  </script>
</body>
</html>